package khleuven.mondzorg.domain;

import java.util.ArrayList;
import java.util.HashSet;

public class DentalCareFacadeImpl implements DentalCareFacade {
	
	private DentalCareApp dca = DentalCareApp.getInstance();

	@Override
	public ArrayList<String> getPreferredDefinitionsAsStringList(){
		return dca.getPreferredEntriesOfTypeAsStringList(EntryType.DEFINITION);		
	}
	
	@Override
	public String getDefinitionExplanation(String defString) {
		String explanation = "";
		Entry entry = dca.getEntry(EntryType.DEFINITION, defString);
		if(entry instanceof Definition){
			Definition currentdef = (Definition)(entry);
			explanation = currentdef.getExplanation();
			explanation = dca.replaceDefinitionsByLinks(explanation, defString);
		}
		return explanation;
	}

	@Override
	public Illustrations getDefinitionIllustrations(String defString) {
		return dca.getEntryIllustrations(EntryType.DEFINITION, defString);
	}
	
	@Override
	public ArrayList<String> getPreferredFaqsAsStringList(){
		return dca.getPreferredEntriesOfTypeAsStringList(EntryType.FAQ);		
	}
	@Override
	public String getFaqExplanation(String faqString) {
		String explanation = "";
		Entry entry = dca.getEntry(EntryType.FAQ, faqString);
		if(entry instanceof Faq){
			Faq currentfaq = (Faq)(entry);
			explanation = currentfaq.getExplanation();
			explanation = dca.replaceDefinitionsByLinks(explanation, "");
		}
		return explanation;
	}
	
	@Override
	public Illustrations getFaqIllustrations(String faqString) {
		return dca.getEntryIllustrations(EntryType.FAQ, faqString);
	}

	@Override
	public Learn getPreferredLearn(Module module, int nr) {
		Learn learn = null;
		HashSet<Entry> allLearns = dca.getPreferredEntriesOfType(EntryType.LEARN);
		for(Object o: allLearns.toArray()){
			if(o instanceof Learn){
				Learn l = (Learn )(o);
				if(l.getModule().equals(module) && l.getSequence() == nr){
					learn = l;
				}
			}			
		}
		return learn;
	}

	@Override
	public Illustrations getLearnIllustrations(Learn learn) {
		Illustrations ills = new Illustrations();
		if(learn != null){
			ills = dca.getEntryIllustrations(learn);
		}
		return ills;
	}

	@Override
	public String getLearnLesson(Learn learn) {
		String lesson = "";
		if(learn != null){
			lesson = dca.replaceDefinitionsByLinks(learn.getLesson(), "");
		}
		return lesson;
	}

	@Override
	public Test getPreferredTest(Module module, int nr) {
		Test test = null;
		HashSet<Entry> allTests = dca.getPreferredEntriesOfType(EntryType.TEST);
		for(Object o: allTests.toArray()){
			if(o instanceof Test){
				Test t = (Test )(o);
				if(t.getModule().equals(module) && t.getSequence() == nr){
					test = t;
				}
			}			
		}
		return test;
	}

	@Override
	public String getTestQuestion(Test test) {
		String question = "";
		if(test != null){
			question = dca.replaceDefinitionsByLinks(test.getQuestion(), "");
		}
		return question;
	}

	@Override
	public String[] getTestAnswers(Test test) {
		String[] answers = new String[]{"","",""};
		if(test != null){
			answers[0] = dca.replaceDefinitionsByLinks(test.getAnswers()[0], "");
			answers[1] = dca.replaceDefinitionsByLinks(test.getAnswers()[1], "");
			answers[2] = dca.replaceDefinitionsByLinks(test.getAnswers()[2], "");
		}
		return answers;
	}

	@Override
	public Illustrations getTestIllustrations(Test test) {
		Illustrations ills = new Illustrations();
		if(test != null){
			ills = dca.getEntryIllustrations(test);
		}
		return ills;
	}




}
